
disableSerialization;

_gmable=player getVariable "GMABLE";If ( player!=user1) then {if (isnil "_gmable") then {killscript=true;breakclic=0;};}; if (killscript) exitwith {killscript=false;};

private ["_source"];

//Début de script OnmapClick
if  (breakclic <= 1 ) then
{
	clic1 = false;

	_display = finddisplay 8000;
	_txt = _display displayctrl 200;

	_txt CtrlSetText "Left click on the map where to clone the group";
	_txt CtrlSetTextColor [0.9,0.9,0.9,1];

	Ctrlshow [200,true];sleep 0.2; CtrlShow [200,false];sleep 0.2;Ctrlshow [200,true];

  //Récuperation des coordonnées de la carte
	onMapSingleClick "
    spawn_x = _pos select 0;
		spawn_y = _pos select 1;
		spawn_z = _pos select 2;
		publicVariable ""spawn_x"";
		publicVariable ""spawn_y"";
		publicVariable ""spawn_z"";
		clic1 = true;
	   onMapSingleClick """";
     ";
	 
		for "_j" from 10 to 0 step -1 do 
		{
		hint format["Click On Map %1, or wait for cancellation",_j];
		sleep 1;
		//hint "pause";
		
			//if (_clic1) exitWith {};
			if (clic1) then
			{
				hint "";
				_j=0;
				clic1 = false;
				_posclick = [spawn_x,spawn_y,spawn_z];
				
        _marker_Take = createMarkerLocal ["Nmarker",_posclick];
				_marker_Take setMarkerShapeLocal "ELLIPSE";
				//		_marker_Take setMarkerTypeLocal "Dot";
				_marker_Take setMarkerSizeLocal [25, 25];
				_marker_Take setMarkerDirLocal 0;
				_marker_Take setMarkerColorLocal "ColorredAlpha";
				
				
        //******************************
        //***** Code come here *********
				//******************************
		
	      hint "Cloning group to new position";

        
				_group=_this select 0;
				_class="";
				_side=side leader _group;
				_newgroup=creategroup _side;
		
		    _groupunit=units _group;
	      _originalvehicles=[];
	      _vehicles=[];
	      _workvehicle=objnull;
	      _code=nil;
	      clonedunit=objnull;
	      
	      //I need an array of all the vehicle of the original group for coyping them
	      _n=1;
	      while {_n<=count _groupunit} do
	      {
	         _unit=_groupunit select (_n-1);
	         if (vehicle _unit!=_unit) then
	         {
	           if !((vehicle _unit) in _originalvehicles) then
	           {
	             _originalvehicles set [count _originalvehicles,vehicle _unit];
	           };
	         };
	         _n=_n+1;
	      };
	      //hint format["%1",_originalvehicles];
	      
	      //Then i create my cloned vehicles at my new position
	      spawnv_x=spawn_x;
	      spawnv_y=spawn_y;
	      spawnv_z=spawn_z;
	      
	      _n=1;
	      while {_n<=count _originalvehicles} do
	      {
	         
	        _class=typeof (_originalvehicles select (_n-1));
	        _veh = _class createVehicle [spawnv_x,spawnv_y,spawnv_z];
	        
	        _vehicles set [count _vehicles,_veh];
	        
	        spawnv_x=spawn_x+5;
	        spawnv_y=spawn_y+5;
	        _n=_n+1;
        };
	      
	      //Lets' clone each of the unit in the group
	      while {(count _groupunit)>0} do
	      {
	      
	         //First we copy the unit 
	         _unit=_groupunit select 0;

	         _class=typeof _unit;
	         //_class createunit [[spawn_x,spawn_y,spawn_z],_newgroup,"clonedunit=this;",Skill _unit,rank _unit];
	         _clonedunit=_newgroup createUnit [_class,[spawn_x,spawn_y,spawn_z],[],0,"FORM"]; 
	         _clonedunit setSkill Skill _unit;
	         _clonedunit setUnitRank rank _unit;
	         spawn_x=spawn_x+2;
	         spawn_y=spawn_y+2;
         
	         //Then the vehicule he is in. 
           //Checking if the unit is in a vehicule
	         if (vehicle _unit!=_unit) then
	         {
	        
	           //Checking if the vehicule has already been created
	           if ((count _vehicles)>0) then
	           {
	             _index=_originalvehicles find vehicle _unit;
               _workvehicle=_vehicles select _index; 
             };
             
   
             //Lets reassign its seat for the new vehicles;
             _vehiok=false;
             if (_unit == Driver (vehicle _unit)) then {_clonedunit assignasdriver _workvehicle; _clonedunit moveindriver _workvehicle;_vehiok=true;};
             if (_unit == Gunner (vehicle _unit)) then {_clonedunit assignasgunner _workvehicle;_clonedunit moveingunner _workvehicle;_vehiok=true;};
             if (_unit == Commander (vehicle _unit)) then {_clonedunit assignascommander _workvehicle;_clonedunit moveincommander _workvehicle;_vehiok=true;};
             //Else lets cargo him and pray for it that there is enough space
             if !(_vehiok) then {_clonedunit assignAsCargo _workvehicle;_clonedunit moveincargo _workvehicle;};
             _vehiok=false;
             
           };
           
	 	         
	         _workvehicle=objnull;
	         _code=nil;
	         _groupunit=_groupunit-[_unit];
	         
	         //sleep 0.15;
        };
        hint "Cloning done";

	      
				//******************************
			
      	sleep 2;
				deletemarker "Nmarker";
				//clean marker
				while {_count>0} do
				{
				  call compile format["deletemarker ""Gmarker%1"";",_count];
				  _count=_count-1;
        };
								
			};
			clic1 = false;
			
	 }; 
		sleep 0.25;
		//hint "";
		breakclic = 0; 
		//	waitUntil {(clic1)};
};
If (true) ExitWith {};
